/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package petstore.view.action.atendimento;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.Globals;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.validator.DynaValidatorActionForm;
import petstore.view.action.*;
import petstore.business.IAtendimentoFacade;
import petstore.model.Anamnese;
import petstore.model.Animal;
import petstore.model.Atendimento;

/**
 *
 * @author Tom Hayden
 */
public class ProntuarioAnamneseAction extends CustomDispatchAction {

    private IAtendimentoFacade atendimentoFacade;
    
    public void setAtendimentoFacade(IAtendimentoFacade atendimentoFacade) {
        this.atendimentoFacade = atendimentoFacade;
    }

    public ActionForward novo(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        request.setAttribute("anamnese", null);
        return mapping.findForward("mesmaPagina");
    }
    
    public ActionForward selecionarAnamnese(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        DynaValidatorActionForm f = (DynaValidatorActionForm) form;
        request.setAttribute("anamnese", this.getAnamneseFromSession((Integer)f.get("id"), request));
        return mapping.findForward("mesmaPagina");
    }
    
    public ActionForward salvar(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        DynaValidatorActionForm f = (DynaValidatorActionForm) form;
        ActionErrors errors = f.validate(mapping, request);

        if (errors.isEmpty()) {
            Anamnese anamnese = this.getAnamnese(f, request);
            Integer idAnamnese = null;
            if (anamnese.getId() != null) {
                idAnamnese = this.atendimentoFacade.alterarAnamnese(anamnese);
            } else {
                idAnamnese = this.atendimentoFacade.incluirAnamnese(anamnese);
            }
            anamnese = this.atendimentoFacade.getAnamnese(idAnamnese);
            request.setAttribute("anamnese", anamnese);
            request.getSession().setAttribute("anamneses", this.atendimentoFacade.listarAnamnesePorAnimal(anamnese.getAnimal()));
            request.setAttribute("msgSucesso", "true");
        } else {
            request.setAttribute(Globals.ERROR_KEY, errors);
            return mapping.getInputForward();
        }

        return mapping.findForward("mesmaPagina");

    }

    private Anamnese getAnamnese(DynaValidatorActionForm f, HttpServletRequest request) {
        Anamnese anamnese = null;
        Integer id = (Integer) f.get("id");
        if (id == null || id.intValue() == 0) {
            anamnese = new Anamnese();
            Animal animal = (Animal)request.getSession().getAttribute("animal");
            anamnese.setAnimal(animal);
            Atendimento atendimento = (Atendimento) request.getSession().getAttribute("atendimento");
            anamnese.setDataAnamnese(atendimento.getDataHoraAtendimento());
        } else {
            anamnese = this.getAnamneseFromSession(id, request);
        }
        anamnese.setDescription(f.getString("description"));
        return anamnese;
    }

    private Anamnese getAnamneseFromSession(Integer idAnamnese, HttpServletRequest request) {
        Anamnese result = null;
        List<Anamnese> anamneses = (List<Anamnese>) request.getSession().getAttribute("anamneses");
        for (Anamnese anamnese : anamneses) {
            if (anamnese.getId().equals(idAnamnese)) {
                result = anamnese;
                break;
            }
        }
        return result;
    }

}
